package com.ts.proj.json;


//import com.google.gson.Gson;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerBorderImpl;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.common.domain.RemoteResult;
import com.ts.proj.entity.CarEntity;
import com.ts.proj.entity.MeasureEntity;
import com.ts.proj.entity.SystemConstantEntity;
import com.ts.proj.entity.excelEntity.MeasureEntityExcel;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.CarService;
import com.ts.proj.service.MeasureService;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.service.SystemConstantService;
import com.ts.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
//说明 字符串：Measure，作为替换字符串 LOWERMeasure

@RestController
@Slf4j
public class MeasureJson extends BaseController{


    @Autowired
    private RestTemplate restTemplate;

    @Value("${restful.url}")
    private String url ;

    @Value("${restful.get-weight}")
    private String getWeight;

    @Value("${restful.write-card}")
    private String writeCard;

    @Value("${restful.print-measure}")
    private String printMeasure;

    @Value("${restful.read-card}")
    private String readCard;

    @Autowired
    private HttpHeaders httpHeaders;

    @Autowired
    private CarService carService;


    @Autowired
    private MeasureService measureService;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private SystemConstantService systemConstantService;
    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/measure-list/{page}/{size}")
    public Map<String,Object> measureList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
            PageData result = new PageData();
        PageData pageData = this.getPageData();
        result.put("deleteFlag", "N");

        if(null!=pageData.get("measureId")&&!"".equals(pageData.get("measureId").toString())){
            result.put("measureId", pageData.get("measureId").toString());
        }
        if(null!=pageData.get("moveTypes")&&!"".equals(pageData.get("moveTypes").toString())){
            result.put("moveType", pageData.get("moveTypes").toString());
        }
        if(null!=pageData.get("carId")&&!"".equals(pageData.get("carId").toString())){
            result.put("carId", pageData.get("carId").toString());
        }
        if(null!=pageData.get("returnSymbol")&&!"".equals(pageData.get("returnSymbol").toString())){
            result.put("returnSymbol", pageData.get("returnSymbol").toString());
        }
        if(null!=pageData.get("closeSymbol")&&!"".equals(pageData.get("closeSymbol").toString())){
            result.put("closeSymbol", pageData.get("closeSymbol").toString());
        }

        if(null!=pageData.get("grossFrom")&&!"".equals(pageData.get("grossFrom").toString())&&!"undefined".equals(pageData.get("grossFrom").toString())){
            result.put("grossFrom", pageData.get("grossFrom").toString());
        }
        if(null!=pageData.get("grossTo")&&!"".equals(pageData.get("grossTo").toString())&&!"undefined".equals(pageData.get("grossTo").toString())){
            result.put("grossTo", pageData.get("grossTo").toString());
        }
        if(null!=pageData.get("tareFrom")&&!"".equals(pageData.get("tareFrom").toString())&&!"undefined".equals(pageData.get("tareFrom").toString())){
            result.put("tareFrom", pageData.get("tareFrom").toString());
        }
        if(null!=pageData.get("tareTo")&&!"".equals(pageData.get("tareTo").toString())&&!"undefined".equals(pageData.get("tareTo").toString())){
            result.put("tareTo", pageData.get("tareTo").toString());
        }
        if(null!=pageData.get("carId")&&!"".equals(pageData.get("carId").toString())){
            result.put("carId", pageData.get("carId").toString());
        }

        if(null!=pageData.get("materialName")&&!"".equals(pageData.get("materialName").toString())){
            result.put("materialName", pageData.get("materialName").toString());
        }
        if(null!=pageData.get("supplierName")&&!"".equals(pageData.get("supplierName").toString())){
            result.put("supplierName", pageData.get("supplierName").toString());
        }
        if(null!=pageData.get("customerName")&&!"".equals(pageData.get("customerName").toString())){
            result.put("customerName", pageData.get("customerName").toString());
        }
        if(null!=pageData.getString("suttleDate")&&!"".equals(pageData.get("suttleDate").toString())&&!"NaN".equals(pageData.get("suttleDate").toString())){
            try{
                String pre = pageData.get("suttleDate").toString().split(" ")[0];
                String last = pageData.get("suttleDate").toString().split(" ")[1];
                result.put("suttleDateFrom",pre+" "+last);
            }catch (Exception e){
            }
        }
        if(null!=pageData.getString("suttleDateTo")&&!"".equals(pageData.get("suttleDateTo").toString())&&!"NaN".equals(pageData.get("suttleDateTo").toString())){
            try{
                String pre = pageData.get("suttleDateTo").toString().split(" ")[0];
                String last = pageData.get("suttleDateTo").toString().split(" ")[1];
                result.put("suttleDateTo",pre+" "+last);
            }catch (Exception e){
            }
        }
        long count = measureService.count(result);
        List<MeasureEntity> measures = measureService.findAll(result,page-1,size);
        result.put("total",count);
        result.put("rows", measures);
        return result;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/measure-ins-upd-del/{id}")
    public Object measureInsUpdDel(@PathVariable("id") String id){

		MeasureEntity measure = measureService.findById(Integer.valueOf(id));
        PageData pageData = this.getPageData();

        Double grossDouble = new Double(0);
        Double tareDouble = new Double(0);

        if(null!=pageData.get("grossWeight")&&!"".equals(pageData.get("grossWeight").toString())){
            grossDouble = Double.valueOf(pageData.get("grossWeight").toString());
        }
        if(null!=pageData.get("tare")&&!"".equals(pageData.get("tare").toString())){
            tareDouble = Double.valueOf(pageData.get("tare").toString());
        }
        Double suttle = grossDouble - tareDouble;
        pageData.put("suttle", suttle);
        List<String> strings = EntityAttributeUtil.entityAttribute(MeasureEntity.class);
        SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);

        for(String attr :strings){
            //字段信息维护
            if(null!=pageData.get(attr)){
                //目前只能手动添加，这里添加所需要增加和修改的字段
                try {
                    Method[] methods = MeasureEntity.class.getDeclaredMethods();
                    for(int i=0;i<methods.length;i++){
                        Method method = methods[i];
                        Class<?>[] returnType = method.getParameterTypes();
                        if(method.getName().equals("set"+StringsUtil.upperCase(attr))&&!method.getName().contains("get")
                                &&!method.getName().equals("setReturnSymbol")
                                &&!method.getName().equals("setCloseSymbol")){
                            if (returnType[0].getName().equals("java.lang.Integer")){
                                method.invoke(measure, Integer.valueOf(pageData.get(attr).toString()));
                            }else if(returnType[0].getName().equals("java.lang.Double")){
                                method.invoke(measure, Double.valueOf(pageData.get(attr).toString()));
                            }
                            else{
                                method.invoke(measure, pageData.get(attr).toString());
                            }
                        }
						if(method.getName().equals("setUserId")){
                            measure.setUserId(systemUserEntity.getId());
                        }
                        if(method.getName().equals("setBelongTo")){
                            measure.setBelongTo(systemUserEntity.getBelongTo());
                        }
						if(method.getName().equals("setPowerLevel")){
                            measure.setPowerLevel(systemUserEntity.getPowerLevel());
                        }
                        if(method.getName().equals("setCloseSymbol")){
                            if(null!=pageData.get("closeSymbol")&&!"".equals(pageData.get("closeSymbol").toString())){
                                try{
                                    Integer closeSymbol = Integer.valueOf(pageData.get("closeSymbol").toString());
                                    measure.setCloseSymbol(closeSymbol);
                                    //关闭
                                    if(closeSymbol==1){
                                        measure.setSuttleDate(DateUtil.getTime());
                                    }
                                }catch (Exception e){

                                }
                            }

                        }
                        if(method.getName().equals("setReturnSymbol")){
                            if(null!=pageData.get("returnSymbol")&&!"".equals(pageData.get("returnSymbol").toString())){
                                //如果退货标记为0，未退货
                                if(Integer.valueOf(pageData.get("returnSymbol").toString()) == 0){
                                    measure.setReturnSymbol(0);
                                    //退货标记为1，已退货
                                }else if(Integer.valueOf(pageData.get("returnSymbol").toString()) == 1){
                                    //退货标记
                                    measure.setReturnSymbol(1);
                                    measure.setCloseSymbol(1);
                                    //出厂
                                    if(measure.getMoveType().equals("OUT")){
                                        Double tareWeight = tareDouble;
                                        log.info("皮重"+measure.getTare()+"");
                                        log.info("毛重"+measure.getGrossWeight()+"");
                                        measure.setGrossWeight(tareWeight);
                                        measure.setSuttle(new Double(0));
                                        measure.setGrossWeightDate(DateUtil.getTime());
                                        measure.setSuttleDate(DateUtil.getTime());
                                    }
                                    //进厂
                                    if(measure.getMoveType().equals("IN")){
                                        Double grossWeight = grossDouble;
                                        log.info("皮重"+measure.getTare()+"");
                                        log.info("毛重"+measure.getGrossWeight()+"");
                                        measure.setTare(grossWeight);
                                        measure.setSuttle(new Double(0));
                                        measure.setTareDate(DateUtil.getTime());
                                        measure.setSuttleDate(DateUtil.getTime());
                                    }
                                }
                            }
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }  catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        measure.setUpdateTime(DateUtil.getTime());
        measure.setIsPublish(systemUserEntity.getNickname());
        //设置修改净重时间
        if(null!=pageData.get("suttleDate")&&!"".equals(pageData.get("suttleDate").toString())){
            measure.setSuttleDate(pageData.get("suttleDate").toString());
        }
        measureService.save(measure);
        return null;
    }

    @RequestMapping(value = "/measure-save")
    public Object measuerSave(){
        PageData pageData = this.getPageData();
        PageData temp = new PageData();
        MeasureEntity measure = null;
        if(null!=pageData.get("measureId")&&!"".equals(pageData.get("measureId").toString())){
            temp.put("deleteFlag", "N");
            temp.put("measureId", pageData.get("measureId").toString());
            List<MeasureEntity> measurelist = measureService.findAll(temp);
            if(measurelist.size()==1){
                measure = measurelist.get(0);
            }else{
                measure = new MeasureEntity();
            }
            measure.setMeasureId(pageData.get("measureId").toString());
        }
        if(null!=pageData.get("grossWeight")&&!"".equals(pageData.getString("grossWeight"))){
            measure.setGrossWeight(Double.valueOf(pageData.get("grossWeight").toString()));
        }
        if(null!=pageData.get("grossTime")&&!"".equals(pageData.getString("grossTime"))){
            measure.setGrossWeightDate(pageData.get("grossTime").toString());
        }
        if(null!=pageData.get("tare")&&!"".equals(pageData.getString("tare"))){
            measure.setTare(Double.valueOf(pageData.get("tare").toString()));
        }
        if(null!=pageData.get("tareTime")&&!"".equals(pageData.getString("tareTime"))){
            measure.setTareDate(pageData.get("tareTime").toString());
        }
        if(null!=pageData.get("tareP")&&!"".equals(pageData.getString("tareP"))){
            measure.setTareP(pageData.get("tareP").toString());
        }
        if(null!=pageData.get("suttle")&&!"".equals(pageData.getString("suttle"))){
            Double d1 = Double.valueOf(pageData.get("grossWeight").toString());
            Double d2 = Double.valueOf(pageData.get("tare").toString());
            Double d3 = d1 - d2;
            if(d3<0)
                d3 = -1 * d3;
            measure.setSuttle(d3);
            measure.setSuttleDate(DateUtil.getTime());
//            measure.setSuttle(Double.valueOf(pageData.get("suttle").toString()));
        }
        if(null!=pageData.get("grossWeightp")&&!"".equals(pageData.getString("grossWeightp"))){
            measure.setGrossWeightP(pageData.get("grossWeightp").toString());
        }
        if(null!=pageData.get("unloadSymbol")&&!"".equals(pageData.getString("unloadSymbol"))){
            if(pageData.get("unloadSymbol").equals("已确认")){
                measure.setUnloadSymbol("1");
            }else{
                measure.setUnloadSymbol("0");
            }
        }
        if(null!=pageData.get("closeSymbol")&&!"".equals(pageData.getString("closeSymbol"))){
            if(pageData.get("closeSymbol").equals("未计量完")){
                measure.setCloseSymbol(0);
            }else if(pageData.get("closeSymbol").equals("计量完毕")){
                measure.setCloseSymbol(1);
            }
        }

        if(null!=pageData.get("returnSymbol")&&!"".equals(pageData.getString("returnSymbol"))){
            if(pageData.get("returnSymbol").equals("已退货")){

                String moveType = measure.getMoveType();
                if(moveType.equals("OUT")){
                    //销售退货
                    measure.setGrossWeight(Double.valueOf(pageData.get("tare").toString()));
                }else if(moveType.equals("IN")){
                    measure.setTare(Double.valueOf(pageData.get("grossWeight").toString()));
                }
                measure.setSuttle(new Double(0));
                measure.setReturnSymbol(1);
                measure.setCloseSymbol(1);
                //采购退货
            }else{
                measure.setReturnSymbol(0);
            }
        }
        if(null!=pageData.get("remarks1")&&!"".equals(pageData.getString("remarks1"))){
            measure.setRemarks1(pageData.get("remarks1").toString());
        }
        if(null!=pageData.get("supplierName")&&!"".equals(pageData.getString("supplierName"))){
            measure.setSupplierName(pageData.get("supplierName").toString());
            if(pageData.get("supplierName").toString().contains("益达")||pageData.get("supplierName").toString().contains("益隆")){
                measure.setMoveType("OUT");
            }else{
                measure.setMoveType("IN");
            }
        }
        if(null!=pageData.get("customerName")&&!"".equals(pageData.getString("customerName"))){
            measure.setCustomerName(pageData.get("customerName").toString());
        }
        if(null!=pageData.get("carId")&&!"".equals(pageData.getString("carId"))){
            measure.setCarId(pageData.get("carId").toString());
        }
        if(null!=pageData.get("materialName")&&!"".equals(pageData.getString("materialName"))){
            measure.setMaterialName(pageData.get("materialName").toString());
        }
        if(null!=pageData.get("storageName")&&!"".equals(pageData.getString("storageName"))){
            measure.setStorageName(pageData.getString("storageName"));
        }
        if(null!=pageData.get("grossWeightSite")&&!"".equals(pageData.getString("grossWeightSite"))){
            measure.setGrossWeightSite(pageData.getString("grossWeightSite"));
        }
        if(null!=pageData.get("tareSite")&&!"".equals(pageData.getString("tareSite"))){
            measure.setTareSite(pageData.getString("tareSite"));
        }
        if(null!=pageData.get("sourceGross")&&!"".equals(pageData.getString("sourceGross"))){
            measure.setSourceGross(pageData.get("sourceGross").toString());
        }
        if(null!=pageData.get("sourceTare")&&!"".equals(pageData.getString("sourceTare"))){
            measure.setSourceTare(pageData.get("sourceTare").toString());
        }
        if(null!=pageData.get("sourceSuttle")&&!"".equals(pageData.getString("sourceSuttle"))){
            measure.setSourceSuttle(pageData.get("sourceSuttle").toString());
        }
        if(null!=pageData.get("remark2")&&!"".equals(pageData.getString("remark2"))){
            measure.setRemarks2(pageData.get("remark2").toString());
        }
        if(null!=pageData.get("max")&&!"".equals(pageData.getString("max"))){
            measure.setMax(pageData.getString("max"));
        }
        measureService.save(measure);
        return null;

    }
	@RequestMapping("/measure-download")
    public void export(HttpServletResponse response) throws IOException {
        PageData param = new PageData();
        PageData pageData = this.getPageData();
        param.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);

        if(null!=pageData.getString("suttleDate")&&!"".equals(pageData.get("suttleDate").toString())&&!"NaN".equals(pageData.get("suttleDate").toString())){
            try{
                String pre = pageData.get("suttleDate").toString().split(" ")[0];
                String last = pageData.get("suttleDate").toString().split(" ")[1];
                param.put("suttleDateFrom",pre+" "+last);
            }catch (Exception e){
            }
        }
        if(null!=pageData.getString("suttleDateTo")&&!"".equals(pageData.get("suttleDateTo").toString())&&!"NaN".equals(pageData.get("suttleDateTo").toString())){
            try{
                String pre = pageData.get("suttleDateTo").toString().split(" ")[0];
                String last = pageData.get("suttleDateTo").toString().split(" ")[1];
                param.put("suttleDateTo",pre+" "+last);
            }catch (Exception e){
            }
        }
        if(null!=pageData.get("measureId")&&!"".equals(pageData.get("measureId").toString())){
            param.put("measureId", pageData.get("measureId").toString());
        }
        if(null!=pageData.get("moveTypes")&&!"".equals(pageData.get("moveTypes").toString())){
            param.put("moveType", pageData.get("moveTypes").toString());
        }
        if(null!=pageData.get("carId")&&!"".equals(pageData.get("carId").toString())){
            param.put("carId", pageData.get("carId").toString());
        }
        if(null!=pageData.get("returnSymbol")&&!"".equals(pageData.get("returnSymbol").toString())){
            param.put("returnSymbol", pageData.get("returnSymbol").toString());
        }
        if(null!=pageData.get("closeSymbol")&&!"".equals(pageData.get("closeSymbol").toString())){
            param.put("closeSymbol", pageData.get("closeSymbol").toString());
        }
        if(null!=pageData.get("materialName")&&!"".equals(pageData.get("materialName").toString())){
            param.put("materialName", pageData.get("materialName").toString());
        }
        if(null!=pageData.get("supplierName")&&!"".equals(pageData.get("supplierName").toString())){
            param.put("supplierName", pageData.get("supplierName").toString());
        }
        if(null!=pageData.get("customerName")&&!"".equals(pageData.get("customerName").toString())){
            param.put("customerName", pageData.get("customerName").toString());
        }

        List<MeasureEntity> entitys = measureService.findAll(param);
        List<MeasureEntityExcel> excelEntities = new ArrayList<>();
        for(MeasureEntity tempEntity :entitys){            
            MeasureEntityExcel excel = new CopyObjectEntity2EntityExcel<MeasureEntityExcel, MeasureEntity>().copy(MeasureEntityExcel.class,MeasureEntity.class,tempEntity);
            excelEntities.add(excel);
        }
//        EasyPoiUtil.exportExcel(excelEntities,"","",MeasureEntityExcel.class, DateUtil.getID()+".xls",response);
        ExportParams params = new ExportParams("计量单详细表","计量单", ExcelType.XSSF);
        params.setStyle(ExcelExportStylerBorderImpl.class);//设置样式,自定义实现IExcelDataHandler<T>
        response.reset();
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-disposition", "attachment; filename=" + new String(DateUtil.getID().getBytes("gb2312"), "ISO-8859-1") + ".xls");
        ServletOutputStream outStream = null;
        Workbook workbook = ExcelExportUtil.exportExcel(params, MeasureEntityExcel.class, excelEntities);
        try {
            outStream = response.getOutputStream();
            workbook.write(outStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outStream.close();
        }
    }
    @RequestMapping(value = "/measure-info")
    public Map<String,String> measureInfo(){

        String name = "";

        PageData pd = this.getPageData();
        if(null!=pd.get("flag")&&!"error".equals(pd.get("flag").toString())&&!"".equals(pd.get("flag").toString())){
            name = pd.get("flag").toString();
        }else{
            String ip = this.getRequest().getRemoteAddr();
            PageData pdConstant = new PageData();
            pdConstant.put("constantKey", "ip-location");
            pdConstant.put("constantValue", ip);
            List<SystemConstantEntity> all = systemConstantService.findAll(pdConstant);

            if(all.size()==1){
                name = all.get(0).getConstantValueDesc();
            }
        }

        String remote = url + "/" +getWeight + "?name="+name;
        RemoteResult forObject = null;
        PageData rsPd = new PageData();
        try{
            forObject = this.restTemplate.getForObject(remote, RemoteResult.class);
            if (forObject.getCode()==1){
                String info = forObject.getMsg();
                rsPd.put("sec", info);
            }
            else if(forObject.getCode()==-1){
                rsPd.put("sec", "异常");
                log.info("ws:" + name + forObject.getMsg());
            }
        }catch (Exception e){
            rsPd.put("sec", "异常");
            log.info("connection:" + name + rsPd.getString("sec"));
        }
        return rsPd;
    }

    @RequestMapping(value = "/measure-read")
    public PageData readMeasureInfo(){

        PageData paramPd = this.getPageData();
        String name = "";
        if(null!=paramPd.get("flag")&&!"".equals(paramPd.get("flag").toString())&&!"error".equals(paramPd.get("flag"))){
            name = paramPd.get("flag").toString();
        }else{
            String ip = this.getRequest().getRemoteAddr();
            PageData pdConstant = new PageData();
            pdConstant.put("constantKey", "ip-location");
            pdConstant.put("constantValue", ip);
            List<SystemConstantEntity> all = systemConstantService.findAll(pdConstant);
            if(all.size()==1){
                try{
                    name = all.get(0).getConstantValueDesc();
                }catch (Exception e){
                    log.info(e.getMessage()+""+all.get(0));
                }

            }
        }

        String remote = url + "/" +readCard + "?name="+name;
        RemoteResult forObject = this.restTemplate.getForObject(remote, RemoteResult.class);
        MeasureEntity measureEntity = new MeasureEntity();
        if(forObject.getCode()==1){
            try{
                measureEntity = JsonUtils.jsonToObject(forObject.getMsg(),MeasureEntity.class);
                String carId = measureEntity.getCarId();
                measureEntity.setRemarks1(carId.substring(0,1));
                measureEntity.setRemarks2(carId.substring(1));
            }catch (Exception e){

            }
        }
        PageData rs = new PageData();
        if(measureEntity==null){
            rs.put("code", -1);
        }else{
            rs.put("code", 1);
        }
        rs.put("obj", measureEntity);
        return rs;
    }
    @RequestMapping(value = "/measure-write/{measureId}")
    public String measureWrite(@PathVariable("measureId") String measureId){
//            MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
//        httpHeaders.setContentType(mediaType);
//      httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());




//        httpHeaders.add("Content-Type", "application/json");
        //
//        PageData temp = new PageData();
//        temp.put("measureId", measureId);
//        temp.put("deleteFlag", "N");
//        List<MeasureEntity> measureEntities = measureService.findAll(temp);
//        MeasureEntity measureEntity = new MeasureEntity();
//        if(measureEntities.size()==1){
//            measureEntity = measureEntities.get(0);
//        }
        //
//        PageData pd = new PageData();
        String ip = this.getRequest().getRemoteAddr();
        PageData pdConstant = new PageData();
        pdConstant.put("constantKey", "ip-location");
        pdConstant.put("constantValue", ip);
        List<SystemConstantEntity> all = systemConstantService.findAll(pdConstant);
        String name = "";
        if(all.size()==1){
            name = all.get(0).getConstantValueDesc();
        }
//        pd.put("name", name);
//        pd.put("info", JsonUtils.objectToJson(measureEntity));

        String remote = url + "/" +writeCard + "?name="+name+"&measureid="+measureId;
//        String remote = url + "/" + writeCard;
//        HttpEntity<String> formEntity = new HttpEntity<String>(JsonUtils.objectToJson(pd), httpHeaders);
        try{

            //
            this.restTemplate.getForObject(remote, RemoteResult.class);
//            this.restTemplate.postForEntity(remote, formEntity,RemoteResult.class);
        }catch (Exception e){
            log.info("写卡错误：");
//            log.info("  post方法Body"+formEntity.getBody());
//            log.info("  post方法Header"+formEntity.getHeaders().toString());
//            log.info("写卡数据："+JsonUtils.objectToJson(pd));
        }

        return "success";
    }

    @RequestMapping(value = "/measure-print/{measureId}")
    public String measurePrint(@PathVariable("measureId") String measureId){
//        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
//        httpHeaders.setContentType(mediaType);
//        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
//        httpHeaders.add("Content-Type", "application/json");

        //
//        PageData temp = new PageData();
//        temp.put("measureId", measureId);
//        List<MeasureEntity> measureEntities = measureService.findAll(temp);
//        MeasureEntity measureEntity = new MeasureEntity();
//        if(measureEntities.size()==1){
//            measureEntity = measureEntities.get(0);
//        }
        //
//        PageData carPd = new PageData();
//        carPd.put("deleteFlag", "N");
//        carPd.put("carIdUnique", measureEntity.getCarId()==null?"":measureEntity.getCarId());
//        List<CarEntity> carEntityList = carService.findAll(carPd);
//        CarEntity carObj = new CarEntity();
//        if(carEntityList.size()==1){
//            carObj = carEntityList.get(0);
//        }
//        PageData entityPd = new PageData();
//        entityPd.put("measureId", measureEntity.getMeasureId());
//
//        entityPd.put("materialName", measureEntity.getMaterialName());
//        entityPd.put("supplierName", measureEntity.getSupplierName());
//        entityPd.put("customerName", measureEntity.getCustomerName());
//        entityPd.put("moveType", measureEntity.getMoveType());
//        entityPd.put("storageName", measureEntity.getStorageName());
//        entityPd.put("grossWeight", measureEntity.getGrossWeight());
//        entityPd.put("grossWeightSite", measureEntity.getGrossWeightSite());
//        entityPd.put("grossWeightP", measureEntity.getGrossWeightP());
//        entityPd.put("grossWeightDate", measureEntity.getGrossWeightDate());
//        entityPd.put("tare", measureEntity.getTare());
//        entityPd.put("tareSite", measureEntity.getTareSite());
//        entityPd.put("tareP", measureEntity.getTareP());
//        entityPd.put("tareDate", measureEntity.getTareDate());
//        entityPd.put("suttle", measureEntity.getSuttle());
//        entityPd.put("suttleDate", measureEntity.getSuttleDate());
//        entityPd.put("unloadSymbol", measureEntity.getUnloadSymbol());
//        entityPd.put("unloadDate", measureEntity.getUnloadDate());
//        entityPd.put("storagep", measureEntity.getStoragep());
//        entityPd.put("returnSymbol", measureEntity.getReturnSymbol());
//        entityPd.put("returnYY", measureEntity.getReturnYY());
//        entityPd.put("closeSymbol", measureEntity.getCloseSymbol());
//        entityPd.put("remarks1", measureEntity.getRemarks1());
//        entityPd.put("remarks2", measureEntity.getRemarks2());
//        entityPd.put("sourceGross", measureEntity.getSourceGross());
//        entityPd.put("sourceTare", measureEntity.getSourceTare());
//        entityPd.put("sourceSuttle", measureEntity.getSourceSuttle());
//        entityPd.put("max", measureEntity.getMax());
//
//        entityPd.put("carId", carObj.getCarId());
//        entityPd.put("driver", carObj.getDriver());
//        entityPd.put("driverLicense", carObj.getDriverLicense());
//        entityPd.put("license", carObj.getLicense());
//        entityPd.put("certificate", carObj.getCertificate());
//        entityPd.put("transportpermit", carObj.getTransportpermit());
//        entityPd.put("carModel", carObj.getCarModel());
//        entityPd.put("axesNumber", carObj.getAxesNumber());
//        entityPd.put("careWeight", carObj.getCareWeight());
//        entityPd.put("transportUnit", carObj.getTransportUnit());

        String ip = this.getRequest().getRemoteAddr();

        PageData pdConstant = new PageData();
        pdConstant.put("constantKey", "ip-location");
        pdConstant.put("constantValue", ip);
        List<SystemConstantEntity> all = systemConstantService.findAll(pdConstant);
        String name = "";
        if(all.size()==1){
            name = all.get(0).getConstantValueDesc();
        }

//        PageData pd = new PageData();
//        pd.put("name", name);
//        pd.put("info", JsonUtils.objectToJson(entityPd));
        String remote = url + "/" +printMeasure + "?name="+name+"&measureid="+measureId;
//        String remote = url + "/" + printMeasure;
//        HttpEntity<String> formEntity = new HttpEntity<String>(JsonUtils.objectToJson(pd), httpHeaders);
        try{
            this.restTemplate.getForObject(remote, RemoteResult.class);
//            this.restTemplate.postForEntity(remote, formEntity, RemoteResult.class);
        }catch (Exception e){
            log.info("打印错误：");
//            log.info("  post方法Body"+formEntity.getBody());
//            log.info("  post方法Header"+formEntity.getHeaders().toString());
//            log.info("打印错误："+JsonUtils.objectToJson(pd));
        }

        //清卡
        try{
//            MeasureEntity clearMeasureEntity = new MeasureEntity();
//            PageData clearPd = new PageData();
//            clearPd.put("name", name);
//            clearPd.put("info", JsonUtils.objectToJson(clearMeasureEntity));
//            String clearRemote = url + "/" + writeCard;
            String clearRemote = url + "/" +writeCard + "?name="+name+"&measureid=-1";
//            HttpEntity<String> clearFormEntity = new HttpEntity<String>(JsonUtils.objectToJson(clearPd), httpHeaders);
            this.restTemplate.getForObject(clearRemote, RemoteResult.class);
//            this.restTemplate.postForEntity(clearRemote, clearFormEntity,RemoteResult.class);
        }catch (Exception e){
            log.info("Clear Error");
        }

        return "success";
    }
}